//
//  JHJMVViewController.m
//  DayByDay
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "JHJMVViewController.h"
#import "CellMVModel.h"
#import <AFNetworking.h>
#import "JHJMVTableViewCell.h"
#import "JHJMVCellTableViewCell.h"
#import "MJRefresh.h"

@interface JHJMVViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, assign)BOOL smallscrollEnable;
@property (nonatomic, copy)NSString *value;
@property (nonatomic, assign)NSInteger page;

@end

@implementation JHJMVViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.array = [NSMutableArray array];
//    [self createData];
    [self createView];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSmallScrollEnable:) name:@"firstScrollEnable" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeValue:) name:@"deliverValue1" object:nil];
    self.page = 1;
    [self createFooter];
}

//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    if (self.array.count == 0) {
//        [self createData];
//    }
//}
- (void)createFooter {
    JHJMVViewController *MVVC = self;
    [self.tableView addFooterWithCallback:^{
        MVVC.page++;
        [MVVC createData];
        
    }];
}

- (void)changeValue:(NSNotification *)noti {
    self.value = [noti object];
    
}

- (void)setValue:(NSString *)value {
    _value = value;
    if (self.array.count == 0) {
        [self createData];
    }

}


- (void)changeSmallScrollEnable:(NSNotification *)noti {
    self.smallscrollEnable = ![[noti object] boolValue];
}

- (void)createData {
//    NSLog(@"sajkfjklsdfjsldf");
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *firstStr = @"http://api.dongting.com/song/singer/";
//    NSString *lastStr = @"/videos?page=1&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5&f=f320&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=40d423da80bb81a4af74287284e5ffc7d78bd326&idfa=76D22EE7-1750-4AA8-84F4-9BED5ED94F03&utdid=VutQ7sZhgbgDAPnEjQ1lv%2B0A&alf=201200&bundle_id=com.ttpod.music&latitude=38.88248&longtitude=121.5395";
    NSString *otherStr = @"&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5&f=f320&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=40d423da80bb81a4af74287284e5ffc7d78bd326&idfa=76D22EE7-1750-4AA8-84F4-9BED5ED94F03&utdid=VutQ7sZhgbgDAPnEjQ1lv%2B0A&alf=201200&bundle_id=com.ttpod.music&latitude=38.88248&longtitude=121.5395";
    NSString *urlStr = [NSString stringWithFormat:@"%@%@/videos?page=%ld%@",firstStr, self.value,self.page, otherStr];
    NSLog(@"%@",urlStr);
    [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        for (NSDictionary *dic in [responseObject valueForKey:@"data"]) {
            CellMVModel *model = [CellMVModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
        }
//        NSLog(@"%@",self.array);
        [self.tableView footerEndRefreshing];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 40) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    UINib *Cellnib = [UINib nibWithNibName:@"JHJMVCellTableViewCell" bundle:[NSBundle mainBundle]];
    [self.tableView registerNib:Cellnib forCellReuseIdentifier:@"ccc"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSmallScrollEnable:) name:@"firstScrollEnable" object:nil];
    self.tableView.showsVerticalScrollIndicator = NO;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    JHJMVCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ccc" forIndexPath:indexPath];
    cell.model = self.array[indexPath.row ];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (!self.smallscrollEnable) {
        scrollView.contentOffset = CGPointZero;
    } else if (scrollView.contentOffset.y <= 0) {
        self.smallscrollEnable = NO;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"bingScrollEnable" object:[NSNumber numberWithBool:self.smallscrollEnable]];
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"MVValue" object:[self.array[indexPath.row] mvList]];
    NSMutableArray *array = [NSMutableArray arrayWithArray:self.array];
    [array addObject:indexPath];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MVValue" object:array];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
